package HouseService;

import domain.House;

public class service {

    private static House[] houses;
    private static int housenums = 0;
    private static int idcounter = 0;

    public service(int size) {
        houses = new House[size];
    }

    public static House findbyid(int findid) {
        for (int i = 0; i < housenums; i++) {//遍历寻找是否存在
            if (findid == houses[i].getId()) {//存在赋值
                return houses[i];
            }
        }

        return null;

    }


    public static boolean del(int delid) {
        int index = -1;//寻找删除下标
        for (int i = 0; i < housenums; i++) {//遍历寻找是否存在
            if (delid == houses[i].getId()) {//存在赋值
                index = i;
            }
        }
        if (index == -1) {
            return false;

        }

        for (int i = index; i < housenums - 1; i++) {
            houses[i] = houses[i + 1];
            //  存在的话在index之后进行数组缩减

        }
        houses[--housenums] = null;//将原本的数组--
        return true;

    }


    public static boolean add(House house) {
        if (housenums == houses.length) {
            System.out.println("房屋已满，不能输入");
            return false;
        }
        houses[housenums++] = house;
        house.setId(++idcounter);
        return true;

    }

    public static House[] list() {
        return houses;
    }
}
